package main

import "fmt"

func main() {

	nums := generateNums(4)
	fmt.Println(nums)
	printNums(nums)

}

func printNums(nums [][]int) {
	rows, cols := len(nums), len(nums)

	if len(nums) == 0 || rows <= 0 || cols <= 0 {
		return
	}

	start := 0

	for cols > start*2 && rows > start*2 {
		printCircle(nums, cols, rows, start)
		start += 1
	}

}

func printCircle(nums [][]int, cols, rows, start int) {

	endX, endY := cols-1-start, rows-1-start

	// left
	for i := 0; i <= endX; i++ {
		n := nums[start][i]
		fmt.Println(n)
	}

	// down
	if start < endY {
		for i := start + 1; i <= endY; i++ {
			fmt.Println(nums[i][endX])
		}
	}

	// right
	if start < endX && start < endY {
		for i := endX - 1; i >= start; i-- {
			fmt.Println(nums[endY][i])
		}
	}

	// up
	if start < endX && start < endY-1 {
		for i := endY - 1; i >= start+1; i-- {
			fmt.Println(nums[i][start])
		}
	}

}

func generateNums(n int) [][]int {
	nums := make([][]int, 4, n)
	counter := 0

	for i := 0; i < n; i++ {
		nums[i] = make([]int, 4, n)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			counter += 1
			nums[i][j] = counter
		}
	}
	return nums
}
